package org.example.springboot.controller;

import org.example.springboot.common.Result;
import org.example.springboot.entity.Teacher;
import org.example.springboot.entity.TeacherAndCourse;
import org.example.springboot.entity.User;
import org.example.springboot.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    @PostMapping("/login")
    public Result login(@RequestBody Teacher teacher) {
        Teacher selected = teacherService.getTeacherByAccount(teacher.getAccount());

        if(selected == null) {
            return Result.error("用户不存在");
        } else if (!selected.getPassword().equals(teacher.getPassword())) {
            return Result.error("账号或者密码错误");
        }else{
            return Result.success(selected);
        }
    }

    @GetMapping("/page")
    public Result getTeachersByPage(@RequestParam("pageNum") int pageNum,
                                 @RequestParam("pageSize") int pageSize,
                                 @RequestParam(value="username",required = false) String username) {
        List<Teacher> teacherList = teacherService.getTeachersByPage(pageNum, pageSize,username);
        int total = teacherService.getTeacherCount(username);
        Map<String, Object> data = new HashMap<>();
        data.put("list", teacherList);  // 用户列表
        data.put("total", total);    // 总记录数

        return Result.success(data);
    }

    @DeleteMapping("/del/{account}")
    public Result delTeacher(@PathVariable String account) {
        teacherService.deleteTeacherByAccount(account);
        return Result.success("删除成功");
    }

    @PostMapping("/add")
    public Result addTeacher(@RequestBody Teacher teacher) {

        Teacher selectTeacher = teacherService.getTeacherByAccount(teacher.getAccount());
        if(selectTeacher!=null)
            return Result.error("用户已存在");

        teacherService.insertTeacher(teacher);
        return Result.success("成功添加");
    }

    @PostMapping("/update")
    public Result updateTeacher(@RequestBody Teacher teacher) {
        teacherService.updateTeacher(teacher);
        return Result.success();
    }

    @GetMapping("/distribution")
    public Result getTeacherDistribution() {
        List<Map<String, Object>> list = teacherService.getTeacherCourseDistribution();
        return Result.success(list);
    }

    @GetMapping("/selectByCourse")
    public Result selectByCourse(@RequestParam String course) {
        List<TeacherAndCourse> teachers = teacherService.getTeachersByCourse(course);
        if (teachers != null && !teachers.isEmpty()) {
            return Result.success(teachers);
        } else {
            return Result.error("404", "未找到相关教师信息");
        }
    }

    @GetMapping("/selectDetail")
    public Result selectTeacherDetail(@RequestParam String account, @RequestParam String course) {
        TeacherAndCourse teacher = teacherService.getTeacherAndCourseByAccount(account, course);
        if (teacher != null) {
            return Result.success(teacher);
        } else {
            return Result.error("教师详情未找到");
        }
    }

    @PostMapping("/password")
    public Result updatePassword(@RequestBody Map<String, String> payload) {
        String account = payload.get("account");
        String newPassword = payload.get("newPassword");

        teacherService.updatePassword(account, newPassword);
        return Result.success();
    }

}
